package gui;


import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;

import card.BuildingCard;
import card.CreatureCard;

import core.Tile;

/**
 * 
 * @author Anton
 *
 * A wrapper for Tile. Adds painting functionality, screen placements and usage of Command.
 * Removes everything used for modifying the Tile itself.
 */

public class GUITile {
	private Tile tile;
	/**
	 * Position on screen, with pos being the center of the tile.
	 */
	private Point pos;
	private Point basePosition;
	private boolean highlighted = false;
	private Polygon polygon;
	private boolean isVisible=true;

	private GUITile(Tile t){
		tile=t;
	}
	
	/**
	 * Creates a GUITile for the given Tile at the coordinate position p.
	 * @param t
	 * @param p Coordinate of the tile, values ranging from 0 to mapsize (aprox 7-13)
	 * @param visible true if inside the visible map
	 */
	public GUITile(Tile t, Point p, boolean visible){
		this(t);
		basePosition=calculateCenterPos(p);
		setOffset(new Point(0, 0));
		generatePolygon();
		isVisible=visible;	
	}
	/**
	 * returns the position on screen
	 * @param p Coordinate of the tile, values ranging from 0 to mapsize (aprox 7-13)
	 */
	private Point calculateCenterPos(Point p){
		int resultX, resultY;
		//Sets top left corner position
		resultX=0;
		resultY=100;
		//offset to right
		resultX=resultX+GUIConstants.TILESIZE*p.x;
		// offset tilted bottom-right
		resultX=resultX+GUIConstants.TILESIZE*p.y/2;
		resultY=resultY+GUIConstants.TILESIZE*p.y*86/100;
		
		return new Point(resultX, resultY);	
	}
	
	/**
	 * generates a polygon with center at this.pos
	 */
	private void generatePolygon(){
		int s=GUIConstants.TILESIZE-3; //some extra space
		int[] x = {pos.x+0, pos.x+s/2, pos.x+s/2, pos.x+0, pos.x-s/2, pos.x-s/2};
		int[] y = {pos.y-(int)(0.5773*s), pos.y-(int)(0.288652*s), pos.y+(int)(0.288652*s), pos.y+(int)(0.5773*s), pos.y+(int)(0.288652*s), pos.y-(int)(0.288652*s)};
		polygon=new Polygon(x, y, 6);
		
	}
	
	public void paint(Graphics g){
		if(isVisible){
			String imageName="";
			int s=GUIConstants.TILESIZE;
			
			if(highlighted)
				imageName="tile_blue.png";
			
			if(tile.getOwningPlayer() != null){
				imageName = tile.getOwningPlayer().getTileImageName();
			}
			else
				imageName="tile_grey.png";
			
			ImageHandler.getInstance().drawImage(pos.x-s/2, pos.y-(int)(0.5773*s), s, (int)(2*0.5773*s), imageName, g);
			
			String landImageName = tile.getLandCard().getImageBig();
			ImageHandler.getInstance().drawImage(pos.x-s/2+6, pos.y-s/2+2, s*2/2-12, s*2/2-4, landImageName, g);
			

			drawBuildings(g);
			drawCreatures(g);
			
			 
		}
	}
	
	private void drawCreatures(Graphics g){
		int s=GUIConstants.TILESIZE;
		int creaturesShown=0;
		int offsetPerCard = 7;
		int baseOffset = 10;
		for(CreatureCard c : tile.getCreatures()){		
			ImageHandler.getInstance().drawImage(pos.x-s/6-offsetPerCard*creaturesShown+baseOffset, pos.y-s/6+offsetPerCard*creaturesShown-baseOffset, s/2, s/2, c.getImageCard(), g);		
			g.setColor(c.getOwner().getColor());
			ImageHandler.getInstance().drawImage(pos.x-s/6-offsetPerCard*creaturesShown+baseOffset, pos.y-s/6+offsetPerCard*creaturesShown-baseOffset, 15, 15, "colorToken.png", c.getOwner().getColor(), g);
			//g.fillOval(pos.x-s/5-5*creaturesShown, pos.y-s/5+5*creaturesShown, s/8, s/8);
			creaturesShown++;
		}
	}
	private void drawBuildings(Graphics g){
		int s=GUIConstants.TILESIZE;
		int buildingsShown=0;
		int offsetPerCard = 7;
		int baseOffset = 10;
		for(BuildingCard c : tile.getBuildings()){
			
			ImageHandler.getInstance().drawImage(pos.x-s/3-offsetPerCard*buildingsShown+baseOffset, pos.y-s/3+offsetPerCard*buildingsShown-baseOffset, s/2, s/2, c.getImageSmall(), g);
			buildingsShown++;
		}
	}
	
	/**
	 * Checks if the Tile contains the point (x,y). Used for checking if the Tile was hit by the mousepointer.
	 * @param x
	 * @param y
	 * @return
	 */
	public boolean contains(int x, int y){
		if(isVisible)
			return polygon.contains(x, y);
		else
			return false;
	}
	
	public void setHighlight(boolean isHighlighted){
		highlighted=isHighlighted;
	}
	
	public Tile getTile(){
		return tile;
	}
	public Point getPosition(){
		return pos;
	}

	/**
	 * sets onscreen offset.
	 * @param p
	 */
	public void setOffset(Point offset){
		pos=new Point(basePosition.x+offset.x, basePosition.y+offset.y);
		generatePolygon();
	}

	
}
